package com.supcon.exchangedata.service.hj;

import com.supcon.exchangedata.pojo.entity.oracle.BaseGeoArea;
import com.supcon.exchangedata.pojo.entity.oracle.BaseGeoAreaDetail;
import com.supcon.exchangedata.pojo.entity.oracle.GisPoint;
import com.supcon.exchangedata.pojo.entity.mysql.operation.hj.HjCleanTransferStation;
import com.supcon.exchangedata.repository.master.MasterBaseAreaRepository;
import com.supcon.exchangedata.repository.master.MasterBaseGeoAreaDetailRepository;
import com.supcon.exchangedata.repository.master.MasterGisPointRepository;
import com.supcon.exchangedata.repository.slave.SlaveGarbageTransferStationRepository;
import com.supcon.exchangedata.repository.slave6.SlaveHjBasicInfo6;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * 垃圾中转站
 *
 * @Author: yangbin
 * @Date: 2022/03/17/15:44
 * @Description:
 */
@Service
@Slf4j
public class GarbageTransferStationService {

    @Resource
    private MasterBaseAreaRepository masterBaseAreaRepository;
    @Resource
    private MasterGisPointRepository masterGisPointRepository;
    @Resource
    private SlaveGarbageTransferStationRepository garbageTransferStationRepository;

    @Resource
    private MasterBaseGeoAreaDetailRepository masterBaseGeoAreaDetailRepository;
    @Resource
    private SlaveHjBasicInfo6 slaveHjBasicInfo6;

    /**
     * 垃圾中转站信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void transGarbageTransferStation() {
        List<HjCleanTransferStation> all = slaveHjBasicInfo6.findAll();
        List<BaseGeoArea> baseGeoAreasByAreaType = masterBaseAreaRepository.findBaseGeoAreaByAreaType(4002L);
        List<BaseGeoArea> removeList = new ArrayList<>();
        List<BaseGeoArea> updateList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(baseGeoAreasByAreaType)) {
            baseGeoAreasByAreaType.forEach((e) -> {

                if (!CollectionUtils.isEmpty(all)) {
                    Iterator<HjCleanTransferStation> iterator = all.iterator();
                    boolean removeFlag = true;
                    while (iterator.hasNext()) {
                        HjCleanTransferStation next = iterator.next();

                        //筛选测试数据
                        if (next.getCleanStationName().equals(e.getAreaName())) {
                            BaseGeoAreaDetail baseGeoAreaDetail = new BaseGeoAreaDetail();

                            removeFlag = false;
                            e.setAreaCode("hjljzzza" + next.getId());
                            e.setUpdateDate(LocalDateTime.now());

                            //中转站地址
                            e.setAddress(next.getCleanStationAddress());
                            //联系人
                            e.setContacts(next.getCleanStationUser());

                            //层高
                            baseGeoAreaDetail.setSubsidiaryInformation1(next.getCleanStationCapacity());
                            //转运类型
                            baseGeoAreaDetail.setSubsidiaryInformation2(next.getCleanStationTransType());
                            //建筑面积
                            baseGeoAreaDetail.setSubsidiaryInformation3(next.getCleanStationBuildArea());

                            //启用时间
                            baseGeoAreaDetail.setSubsidiaryInformation4(next.getCleanStationOperationStime());
                            //占地面积
                            baseGeoAreaDetail.setSubsidiaryInformation5(next.getCleanStationCoverArea());
                            //营运
                            baseGeoAreaDetail.setSubsidiaryInformation6(next.getCleanStationOperationStime());
                            e.setBaseGeoAreaDetail(baseGeoAreaDetail);

                            String cleanStationLongitudeLatitud = next.getCleanStationLongitudeLatitud();

                            String[] split = cleanStationLongitudeLatitud.split(",");
                            if (split.length == 2) {

                                Double lat = Double.valueOf(split[1]);
                                e.setLatitude(lat);
                                Double longitude = Double.valueOf(split[0]);
                                e.setLongitude(longitude);
                            }
                            updateList.add(e);
                            iterator.remove();
                            break;
                        }
                    }
                    if (removeFlag) {
                        removeList.add(e);
                    }
                }


            });
        }

        deleteAreaAndPoint(removeList);

        updateTransGarbageStationData(updateList);
        if (!CollectionUtils.isEmpty(all)) {
            int i = 0;
            for (HjCleanTransferStation station : all) {
                BaseGeoArea area = new BaseGeoArea();
                GisPoint gisPoint = new GisPoint();
                i++;
                gisPoint.setCode("hjljzzza" + station.getId());
                area.setAreaCode("hjljzzza" + station.getId());
                area.setParentId(20898L);
                area.setAreaType(4002L);
                area.setAreaName(station.getCleanStationName());
                area.setFullLevelName("环境→垃圾中转站→" + station.getCleanStationName());
                area.setUpdator("jpa-progarm");
                area.setUpdateDate(LocalDateTime.now());
                BaseGeoAreaDetail baseGeoAreaDetail = new BaseGeoAreaDetail();
                //中转站地址
                area.setAddress(station.getCleanStationAddress());
                //联系人
                area.setContacts(station.getCleanStationUser());
                //层高
                baseGeoAreaDetail.setSubsidiaryInformation1(station.getCleanStationCapacity());
                //转运类型
                baseGeoAreaDetail.setSubsidiaryInformation2(station.getCleanStationTransType());
                //建筑面积
                baseGeoAreaDetail.setSubsidiaryInformation3(station.getCleanStationBuildArea());

                //启用时间
                baseGeoAreaDetail.setSubsidiaryInformation4(station.getCleanStationOperationStime());
                //占地面积
                baseGeoAreaDetail.setSubsidiaryInformation5(station.getCleanStationCoverArea());
                //营运
                baseGeoAreaDetail.setSubsidiaryInformation6(station.getCleanStationOperationStime());

                gisPoint.setObjType(1L);
                gisPoint.setName(station.getCleanStationName());
                gisPoint.setObjCode("hjljzzza" + station.getId());
                gisPoint.setBusinessCategory("4002");
                gisPoint.setRoad(station.getCleanStationAddress());
                masterBaseAreaRepository.save(area);
                gisPoint.setObjId(area.getId());
                masterGisPointRepository.save(gisPoint);
                baseGeoAreaDetail.setAreaId(area.getId());
                masterBaseGeoAreaDetailRepository.save(baseGeoAreaDetail);
            }

        }

    }

    /**
     * 更新垃圾中转站相关图层信息
     *
     * @param updateList
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateTransGarbageStationData(List<BaseGeoArea> updateList) {
        if (!CollectionUtils.isEmpty(updateList)) {
            updateList.forEach((e) -> {
                masterBaseAreaRepository.save(e);
                //更新gisPoint
                if (e.getLongitude() != null) {
                    GisPoint distinctByObjId = masterGisPointRepository.findDistinctByObjId(e.getId());
                    if (distinctByObjId == null) {
                        distinctByObjId = new GisPoint();
                    }
                    distinctByObjId.setObjId(e.getId());
                    distinctByObjId.setObjCode(e.getAreaCode());
                    distinctByObjId.setCode(e.getAreaCode());
                    distinctByObjId.setBusinessCategory(String.valueOf(e.getAreaType()));
                    distinctByObjId.setYwCategory(String.valueOf(e.getAreaType()));
                    distinctByObjId.setLatitude(e.getLatitude());
                    distinctByObjId.setLongitude(e.getLongitude());
                    distinctByObjId.setRoad(e.getAddress());
                    distinctByObjId.setName(e.getAreaName());
                    masterGisPointRepository.save(distinctByObjId);
                }
                BaseGeoAreaDetail distinctByAreaId = masterBaseGeoAreaDetailRepository.findDistinctByAreaId(e.getId());
                if (distinctByAreaId == null) {
                    distinctByAreaId = new BaseGeoAreaDetail();
                }
                BaseGeoAreaDetail baseGeoAreaDetail = e.getBaseGeoAreaDetail();
                if (baseGeoAreaDetail != null) {
                    //层高
                    distinctByAreaId.setSubsidiaryInformation1(baseGeoAreaDetail.getSubsidiaryInformation1());
                    //转运类型
                    distinctByAreaId.setSubsidiaryInformation2(baseGeoAreaDetail.getSubsidiaryInformation2());
                    //建筑面积
                    distinctByAreaId.setSubsidiaryInformation3(baseGeoAreaDetail.getSubsidiaryInformation3());

                    //启用时间
                    distinctByAreaId.setSubsidiaryInformation4(baseGeoAreaDetail.getSubsidiaryInformation4());
                    //产权归属
                    distinctByAreaId.setSubsidiaryInformation5(baseGeoAreaDetail.getSubsidiaryInformation5());
                    //是否办公
                    distinctByAreaId.setSubsidiaryInformation6(baseGeoAreaDetail.getSubsidiaryInformation6());
                    //转运能力
                    distinctByAreaId.setSubsidiaryInformation7(baseGeoAreaDetail.getSubsidiaryInformation7());
                    //占地面积
                    distinctByAreaId.setSubsidiaryInformation8(baseGeoAreaDetail.getSubsidiaryInformation8());
                }
                distinctByAreaId.setAreaId(e.getId());
                masterBaseGeoAreaDetailRepository.save(distinctByAreaId);

            });
        }
    }

    /**
     * 删除不需要的图层点位信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteAreaAndPoint(List<BaseGeoArea> removeList) {
        if (!CollectionUtils.isEmpty(removeList)) {
            removeList.forEach((e) -> {
                masterBaseAreaRepository.deleteById(e.getId());
                masterGisPointRepository.deleteByObjIdAndYwCategory(e.getId(), String.valueOf(e.getAreaType()));
                masterBaseGeoAreaDetailRepository.deleteByAreaId(e.getId());
            });
        }
    }
}
